package cn.itsource.smartcampus.controller;

import cn.itsource.smartcampus.domain.User;
import cn.itsource.smartcampus.exception.GlobalException;
import cn.itsource.smartcampus.service.UserService;
import cn.itsource.smartcampus.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private UserService us;
    
    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user){
        try{
            String token = us.login(user);
            return AjaxResult.me().setSuccess(true).setResultObj(token);
        }catch(GlobalException ge){
            return AjaxResult.me().setSuccess(false).setMessage(ge.getMessage());
        }catch(Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常，请联系管理员");
        }
    }
}
